
package com.suarte.webapp.action;
import com.suarte.core.Employee;
import com.suarte.core.Remission;
import com.suarte.core.service.RemissionManager;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.appfuse.model.User;
import org.appfuse.service.GenericManager;

/**
 * @date   Feb 18, 2011
 * @author Ggutierrez
 */
public class RemissionForm extends BasePage implements Serializable {

    private RemissionManager remissionManager;
    private GenericManager<Employee, Long> employeeManager;
    private Long id;
    private Remission remission;

    public void setRemissionManager(RemissionManager remissionManager) {
        this.remissionManager = remissionManager;
    }

    public void setEmployeeManager(GenericManager<Employee, Long> employeeManager) {
        this.employeeManager = employeeManager;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Remission getRemission() {
        return remission;
    }

    public void setRemission(Remission remission) {
        this.remission = remission;
    }


    public String add() {
        if (id != null) {
            remission = remissionManager.get(id);
        } else {
            remission = new Remission();
        }

        return "add";
    }

    public String delete() {
        remissionManager.remove(remission.getId());
        addMessage("quotation.deleted");

        return "list";
    }

    public String edit() {
        System.out.println("Entra Edit Remission");
        if (id != null) {
            remission = remissionManager.get(id);
        } else {
            remission = new Remission();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (remission.getId() == null);
        HttpServletRequest request = getRequest();
        Date date = new Date();
        User requestUser = userManager.getUserByUsername(request.getRemoteUser());

        if (isNew){
            remission.setCreatedBy(requestUser);
            remission.setDate(date);
        }
        remission.setModifiedBy(requestUser);
        remission.setModifiedOn(date);
        remissionManager.save(remission);

        String key = (isNew) ? "remission.added" : "remission.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }

    public List<Employee> getResponsibles() {
        return employeeManager.getAll();
    }
}
